<template>
  <div class="detail">
    <detail-banner
      :sightName="sightName"
      :bannerImg="bannerImg"
      :gallaryImgs="gallaryImgs"
    ></detail-banner>
    <detail-header></detail-header>
    <div class="content">
      <detail-list :list="list"></detail-list>
    </div>
  </div>
</template>

<script>
  import DetailBanner from './components/banner'
  import DetailList from './components/list'
  import DetailHeader from './components/header'
  import axios from 'axios'
  export default {
    name: "Detail",
    components:{
      DetailBanner,
      DetailList,
      DetailHeader
    },
    data (){
      return {
        lastId:'',
        sightName:'',
        bannerImg:'',
        gallaryImgs:[],
        list:[]
      }
    },
    methods:{
      getDetailInfo(){
        axios.get('/api/detail.json',{
          params:{
            id:this.$route.params.id
          }
        }).then(this.handleGetDataSucc);
      },
      handleGetDataSucc(res){
        res = res.data;
        if(res.ret && res.data){
          const data = res.data;
          this.sightName = data.sightName;
          this.bannerImg = data.bannerImg;
          this.gallaryImgs = data.gallaryImgs;
          console.log(this.gallaryImgs);
          this.list = data.categoryList;
        }
      }
    },
    mounted(){
      console.log(this.$route.params.id);
      //页面挂载完成，给lastId赋值，存入缓存
      this.lastId = this.$route.params.id;
      this.getDetailInfo();
    },
    //判断请求路径的参数，如果这次跟上次请求的相等，就从内存里取组件，如果不等，就向后台发送请求
    activated(){
      //页面重新加载时，如果缓存中的lastId的值和重新加载时不一致，就发送ajax请求新的页
      //如果一致，就从缓存中取组件
      if(this.lastId !== this.$route.params.id){
        this.getDetailInfo();
      }
    }
  }
</script>

<style scoped lang="stylus">
  .content
    height:50rem
</style>
